#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 21-Apr-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
using Thn.Serialization;
using Thn.Serialization.Binary;
#endregion

namespace Thn.Communication.Teleporting
{
    /// <summary>
    /// This codec uses <see cref="BinarySerializer"/> for serialization. Params are required to
    /// have <see cref="SavableAttribute"/>.
    /// </summary>
#if DEBUG 
    public
#else
    internal
#endif 
    class BinaryCodec : IBinaryCodec
    {
        #region Fields
        BinarySerializer mSerializer = null;
        #endregion

        #region Encode
        /// <summary>
        /// Serialize object.
        /// </summary>
        public byte[] Encode(object value)
        {
            return mSerializer.Serialize(value).AsBytes;
        }

        /// <summary>
        /// Serialize an arbitrary array of objects.
        /// </summary>
        public byte[] Encode(params object[] values)
        {
            return mSerializer.Serialize(values).AsBytes;
        }
        #endregion

        #region Decode
        /// <summary>
        /// Deserialize parameters
        /// </summary>
        public object Decode(byte[] encodedArgs)
        {
            return mSerializer.Deserialize(encodedArgs);
        }
        #endregion

        #region To String
        /// <summary>
        /// Converts an arbitrary array of bytes to string.
        /// </summary>
        public string ToString(byte[] data)
        {
            return Convert.ToBase64String(data);
        }
        #endregion

        #region From String
        /// <summary>
        /// Converts from a string back to byte array.
        /// </summary>
        public byte[] FromString(string data)
        {
            return Convert.FromBase64String(data);
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public BinaryCodec()
        {
            mSerializer = new BinarySerializer();            
        }
        #endregion
    }
}
